//
//  ShareView.m
//  Azkar
//
//  Created by 王易 on 2018/8/28.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "ShareView.h"
#import "ReportView.h"
#import "ReportModel.h"
#import <MessageUI/MessageUI.h>

/**
 分享View
 */
@interface ShareView()<MFMessageComposeViewControllerDelegate>
@property (nonatomic,strong) NSArray *dataArr;
@property (nonatomic,weak) MyRelativeLayout *reportLayout;
@end
@implementation ShareView
- (NSArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = @[@[TLString(@"Facebook"),Image(@"facebook_share_ic")],@[TLString(@"Email"),Image(@"email_share_ic")],@[TLString(@"Message"),Image(@"message_share_ic")]];
    }
    return _dataArr;
}
- (instancetype)init
{
    if (self == [super init]) {
        
        [self initUI];
        
    }
    return self;
    
}
- (void)initUI
{
    self.backgroundColor = [UIColor clearColor];
    
    self.frame = [UIScreen mainScreen].bounds;
    
    MyRelativeLayout *rootLayout = [MyRelativeLayout new];
    rootLayout.myBottom = 0;
    rootLayout.myTop = 0;
    rootLayout.myHorzMargin= 0;
    rootLayout.backgroundColor = [UIColor clearColor];
    [self addSubview:rootLayout];
    
    [rootLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        [[SKPopupHelper shareInstance] pop_hide];
    }];
    
    MyRelativeLayout *bgView = [MyRelativeLayout new];
    bgView.myHorzMargin = 0;
    bgView.myBottom = 0;
    bgView.myHeight = [PhoneUtil tabbarAdjustHeight];
    bgView.backgroundColor = [UIColor whiteColor];
    [rootLayout addSubview:bgView];
    
    
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.wrapContentHeight = YES;;
    contentLayout.myHorzMargin = 0;
    contentLayout.myBottom = [PhoneUtil tabbarAdjustHeight];;
    contentLayout.backgroundColor = [UIColor whiteColor];
    [rootLayout addSubview:contentLayout];
    
    
    
    
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.myHorzMargin = scrollView.myTop = 0;
    scrollView.wrapContentHeight = YES;
    [contentLayout addSubview:scrollView];
    
    MyFlowLayout *flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Horz    arrangedCount:1];
    flowLayout.myHorzMargin = flowLayout.myTop = 0;
    flowLayout.wrapContentSize = YES;
    flowLayout.leadingPadding = flowLayout.trailingPadding = 20;
    flowLayout.subviewHSpace = 10;
    flowLayout.padding = UIEdgeInsetsMake(24, 0, 16, 0);
    [scrollView addSubview:flowLayout];
    
    [self.dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MyRelativeLayout *childLayout = [MyRelativeLayout new];
        childLayout.wrapContentSize = YES;
        childLayout.padding = UIEdgeInsetsMake(0, 10, 0, 10);
        [flowLayout addSubview:childLayout];
        
        NSArray *array = obj;
        UIImageView *imageV = [UIImageView new];
        imageV.myTop = 0;
        imageV.myLeading = 0;
        imageV.myWidth = imageV.myHeight = 44;
        imageV.layer.cornerRadius = 22;
        imageV.layer.masksToBounds = YES;
        imageV.image = array[1];
        [childLayout addSubview:imageV];
        
        UILabel *title = [UILabel new];
        title.wrapContentSize = YES;
        title.textColor= Color(@"#6F6F6F");
        title.font = FontSize(12);
        title.topPos.equalTo(imageV.bottomPos).offset(6);
        title.myCenterX = 0;
        title.text = array[0];
        [childLayout addSubview:title];
        
        [childLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
            if (self.model) {
                [AzkarApi shareBless:self.model.ID success:nil failure:nil];
            }
            else if (self.pbModel){
                [AzkarApi sharePBBless:self.pbModel.ID success:nil failure:nil];
            }
            if (self.block) {
                self.block();
            }
            switch (idx) {
                case 0:// Facebook
                {
                    
                    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
                    content.quote = self.model?self.model.content:self.pbModel.content;
                    content.contentURL = [NSURL URLWithString:@"https://itunes.apple.com/us/app/id1402389078?mt=8"];
                    
                    //                    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
                    //                    dialog.fromViewController = [UIViewController currentViewController];
                    //                    dialog.content = content;
                    //                    dialog.mode = FBSDKShareDialogModeShareSheet;
                    //                    [dialog show];
                    [FBSDKShareDialog showFromViewController:[UIViewController currentViewController] withContent:content delegate:nil];
                    [[SKPopupHelper shareInstance] pop_hide];
                }
                    break;
                case 1:// email
                {
                    NSMutableString *mailUrl = [NSMutableString string];
                    [mailUrl appendString:@"mailto: ?"];
                    [mailUrl appendString:@"&subject=iMuslim"];
                    [mailUrl appendString:[NSString stringWithFormat:@"&body=%@\n%@",self.model?self.model.content:self.pbModel.content,@"https://itunes.apple.com/us/app/id1402389078?mt=8"]];
                    NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
                    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
                    [[SKPopupHelper shareInstance] pop_hide];
                }
                    break;
                case 2:
                {
                    MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
                    vc.messageComposeDelegate = self;
                    vc.body = [NSString stringWithFormat:@"%@\n%@",self.model?self.model.content:self.pbModel.content,@"https://itunes.apple.com/us/app/id1402389078?mt=8"];
                    UIViewController *ctr = [UIViewController currentViewController];
                    [ctr presentViewController:vc animated:YES completion:nil];
                    [[SKPopupHelper shareInstance] pop_hide];
                }
                    break;
                default:
                    break;
            }
        }];
        
    }];
    
    MyRelativeLayout *relativeLayout = [MyRelativeLayout new];
    self.reportLayout = relativeLayout;
    relativeLayout.myHorzMargin = 0;
    relativeLayout.topPos.equalTo(scrollView.bottomPos);
    relativeLayout.wrapContentHeight = YES;
    relativeLayout.padding = UIEdgeInsetsMake(20, 0, 20, 0);
    MyBorderline  *bld2 = [[MyBorderline alloc] initWithColor:Color(@"#EDEDED")];
    bld2.headIndent = bld2.tailIndent = 16;
    relativeLayout.topBorderline = bld2;
    [contentLayout addSubview:relativeLayout];
    
    MyRelativeLayout *reportLayout = [MyRelativeLayout new];
    reportLayout.myLeading = 0;
    reportLayout.myTop = 0;
    reportLayout.leadingPadding = reportLayout.trailingPadding = 20;
    reportLayout.wrapContentSize = YES;
    [relativeLayout addSubview:reportLayout];
    
    
    UIImageView *reportImage = [UIImageView new];
    reportImage.myWidth = reportImage.myHeight = 44;
    reportImage.myLeading = reportImage.myTop = 0;
    reportImage.layer.cornerRadius = 22;
    reportImage.layer.masksToBounds = YES;
    reportImage.image = Image(@"inform_btn");
    [reportLayout addSubview:reportImage];
    
    UILabel *reportTitle = [UILabel new];
    reportTitle.textColor= Color(@"#6F6F6F");
    reportTitle.font = FontSize(12);
    reportTitle.text = TLString(@"social_inform");
    reportTitle.myCenterX = 0;
    reportTitle.topPos.equalTo(reportImage.bottomPos).offset(6);
    reportTitle.wrapContentSize = YES;
    [reportLayout addSubview:reportTitle];
    
    //举报
    WeakSelf
    [reportLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        StrongSelf
        [[SKPopupHelper shareInstance] pop_hide];
        
        ReportView *reportView = [ReportView new];
        reportView.lang_type = self.lang_type;
        reportView.isPublic = self.isPublic;
        reportView.Id = self.Id;
        [reportView showView];
    }];
    
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancel changeTitle:TLString(@"Cancel")];
    cancel.titleLabel.font = FontSize(17);
    [cancel changeTitleColor:Color(@"#525252")];
    cancel.backgroundColor = Color(@"#F9F9F9");
    cancel.myHorzMargin = 0;
    cancel.topPos.equalTo(relativeLayout.bottomPos);
    cancel.myHeight = 56;
    [contentLayout addSubview:cancel];
    
    [cancel setHyb_onTouchUp:^(UIButton *sender) {
        
        [[SKPopupHelper shareInstance] pop_hide];
        
    }];
}
- (void)setIsReport:(BOOL)isReport
{
    _isReport = isReport;
    self.reportLayout.myVisibility = self.isReport ? MyVisibility_Visible : MyVisibility_Gone;
    
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [controller dismissViewControllerAnimated:YES completion:nil];
}


- (void)showView
{
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    [[SKPopupHelper shareInstance] skpop_FlyInFromBottom:self defaultHideEnable:YES alphaBG:0.3];
}
- (void)showViewWith:(BlessModel*)model{
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    self.model = model;
    [[SKPopupHelper shareInstance] skpop_FlyInFromBottom:self defaultHideEnable:YES alphaBG:0.3];
    
}
@end
